
var layer = layui.layer //弹层
    ,table = layui.table //表格
    form = layui.form,$ = layui.jquery,laydate = layui.laydate,cbLists=[], id = getQueryVariable("id");


//主表数据数据
let dataList = [];
//明细数据
let detailList = [];
layer.ready(function () {
    //获取详情信息
    $.ajax({
        url:"payment/getRefundInfo?id="+id,
        success:function(res){
            if(res.status == 200){
                //主表信息赋值
                if(null != res.data){
                    //主表数据数据
                    if(!isEmpty(res.data) && res.data.length > 0){
                        dataList =  res.data;
                        table.reload('mainstableid',{data : dataList});

                        //明细数据
                        if(!isEmpty(dataList[0].refundList) && dataList[0].refundList.length > 0){
                            detailList = dataList[0].refundList;
                            table.reload('childinformationtableid',{data : detailList});
                        }
                    }
                }
                return false;
            }else {
                layer.msg(res.msg, {icon: 5});
            }
        }
    });
    form.render();

});


table.render({
    elem: '#mains'
    ,cols: [[
        {field:'billNo', title:'付款单号',sort:true,width: 120}
        ,{field:'time', title:'应付日期',sort:true,width: 120}
        ,{field:'supplierName', title:'收款单位',sort:true,width: 120}
        ,{field:'bankName', title:'收款银行',sort:true,width: 120}
        ,{field:'bankAccount', title:'收款账号',sort:true,width: 120}
        ,{field:'currency', title:'币种',sort:true,width: 120}
        ,{field:'rmbRate', title:'人民币汇率',sort:true,width: 120}
        ,{field:'usdRate', title:'美金汇率',sort:true,width: 120}
        ,{field:'companyName', title:'制单公司',sort:true,width: 120}
        ,{field:'operation', title:'操作员',sort:true,width: 120}
        ,{field:'memo', title:'备注',sort:true,width: 120}
    ]]
    ,id: 'mainstableid'
    ,height: 300
    ,done: function (res, curr, count) {
        table.render({
            elem: '#childinformation'
            ,toolbar: "#childinformationtoolbar"
            ,cols: [[
                /*{checkbox: true, fixed: true},*/
                {field: 'invoiceNumber',  title: '单据编号',sort:true},
                {field: 'purchaseNo',  title: '采购合同号',sort:true},
                {field: 'contractNo',  title: '外销合同号',sort:true},
                {field: 'amount',  title: '退款金额 ',edit:'text'},
                {field: 'memo', title:'备注',edit:'text'}
            ]]
            ,id: 'childinformationtableid'
            ,height: 300
        });
    }
});

//单击行事件
table.on('row(mainstable)', function (obj) {
    let id = obj.data.id;
    for(let i in dataList){
        if(dataList[i]["id"] == id){
            let data = dataList[i];
            if(!isEmpty(data.refundList) && data.refundList.length > 0){
                detailList = data.refundList;
            }else {
                detailList = [];
            }
        }
    }
    table.reload('childinformationtableid',{data : detailList});
});

/*table.on('tool(childinformationtable)', function (obj) {
    if (obj.event === 'del') {
        layer.confirm('真的删除行么', function (index) {
            //需要删除的ID：data.id
            obj.del();
            layer.msg("删除成功", {icon: 6});
            layer.close(index);
        });
    }
});*/

table.on('edit(childinformationtable)',function (obj) {
    //旧值
    let old=$(this).prev().text();
    //判断数据类型 ,,,,,,,
    if((obj.field== "amount")&&!isNumber(obj.value)) {
        layer.msg('请输入数字');
        $(this).val(old);
        return;
    }
    if(obj.field == "amount"){
        let newData = obj.data;
        obj.data["amount"] = obj.value;
        updateData(newData);
    }
    if(obj.field == "memo"){
        let newData = obj.data;
        obj.data["memo"] = obj.value;
        updateData(newData);
    }
});

function updateData(newData){
    for(let i in dataList){
        if(dataList[i].id == newData.parentId){
            let details = dataList[i].refundList;
            for(let j in details){
                if(details[j].detailId == newData.detailId){
                    details[j] = newData;
                    dataList[i].refundList = details;
                    break;
                }
            }
        }
    }
}

$("#choose").on("click",function () {
    //向后台组装数据
    let result = JSON.stringify(dataList);
    $.ajax({
        type:"POST",
        dataType:"json",
        contentType: 'application/json',
        data:result,
        url:"payment/addRefund",
        success:function(res){
            if(res.status == 200){
                //执行接口
                layer.msg("保存成功", {icon: 6});
                setTimeout(function () {
                    //保存成功则关闭此页面
                    let index = parent.layer.getFrameIndex(window.name);
                    parent.layer.close(index);
                },1000)
            }else {
                layer.msg(res.msg, {icon: 5});
            }
            return false;
        }
    });

})
$("#shutdown").on("click",function () {
    let index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
});

function isNumber(val) {
    var regPos = /^\d+(\.\d+)?$/; //非负浮点数
    var regNeg = /^(-(([0-9]+\.[0-9]*[1-9][0-9]*)|([0-9]*[1-9][0-9]*\.[0-9]+)|([0-9]*[1-9][0-9]*)))$/; //负浮点数
    if (regPos.test(val) || regNeg.test(val)) {
        return true;
    } else {
        return false;
    }
}

//字段是否为空或者零
function isEmpty(obj){
    if(typeof obj == "undefined" || obj == null || obj == "" || obj == 0){
        return true;
    }else{
        return false;
    }
}